package com.yami.shop.user.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.user.common.dao.UserRightsMapper;
import com.yami.shop.user.common.model.UserLevelRights;
import com.yami.shop.user.common.model.UserRights;
import com.yami.shop.user.common.model.UserRightsLang;
import com.yami.shop.user.common.service.UserLevelRightsService;
import com.yami.shop.user.common.service.UserRightsLangService;
import com.yami.shop.user.common.service.UserRightsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员等级
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserRightsServiceImpl extends ServiceImpl<UserRightsMapper, UserRights> implements UserRightsService {
    private UserRightsMapper userRightsMapper;
    private UserLevelRightsService userLevelRightsService;
    private UserRightsLangService userRightsLangService;

    @Override
    public Boolean setStatua(Long rightsId, Integer status) {
        return userRightsMapper.setStatua(rightsId,status);
    }

    @Override
    public List<Long> getUserRightsIdListByLevelId(Long id ,Integer levelType) {
        return userRightsMapper.getUserRightsIdListByLevelId(id,levelType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRights(Long rightsId) {
        boolean isSuccess = this.removeById(rightsId);
        userLevelRightsService.remove(new LambdaQueryWrapper<UserLevelRights>().eq(UserLevelRights::getRightsId,rightsId));
        return isSuccess;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean  savaUserRights(UserRights userRights) {
        boolean save = this.save(userRights);
        // 保存权益国际化信息
        for (UserRightsLang userRightsLang : userRights.getUserRightsLangList()) {
            userRightsLang.setRightsId(userRights.getRightsId());
        }
        boolean saveBatch = userRightsLangService.saveBatch(userRights.getUserRightsLangList());
        if (save && saveBatch){
            return true;
        }
        return false;
//        List<UserRightsLang> userRightsLangList = new ArrayList<>();
//        UserRightsLang userRightsLangCn = new UserRightsLang();
//        userRightsLangCn.setRightsId(userRights.getRightsId());
//        userRightsLangCn.setLang(0);
//        userRightsLangCn.setRightsName(userRights.getRightsNameCn());
//        userRightsLangCn.setDescription(userRights.getDescriptionCn());
//        userRightsLangList.add(userRightsLangCn);
//        UserRightsLang userRightsLangEn = new UserRightsLang();
//        userRightsLangEn.setRightsId(userRights.getRightsId());
//        userRightsLangEn.setLang(1);
//        userRightsLangEn.setRightsName(userRights.getRightsNameEn());
//        userRightsLangEn.setDescription(userRights.getDescriptionEn());
//        userRightsLangList.add(userRightsLangEn);
//        userRightsLangService.saveBatch(userRightsLangList);
    }

    @Override
    public UserRights getRightsById(Long rightsId) {
        UserRights rights = this.getById(rightsId);
        // 获取国际化信息
        List<UserRightsLang> userRightsLangList = userRightsLangService.list(new LambdaQueryWrapper<UserRightsLang>()
                .eq(UserRightsLang::getRightsId, rightsId));
        rights.setUserRightsLangList(userRightsLangList);
//        for (UserRightsLang userRightsLang : userRightsLangList) {
//            if (userRightsLang.getLang() == 0){
//                rights.setRightsNameCn(userRightsLang.getRightsName());
//                rights.setDescriptionCn(userRightsLang.getDescription());
//                continue;
//            }
//            if (userRightsLang.getLang() == 1){
//                rights.setRightsNameEn(userRightsLang.getRightsName());
//                rights.setDescriptionEn(userRightsLang.getDescription());
//            }
//
//        }
        return rights;
    }

    @Override
    public IPage<UserRights> getRightsPage(PageParam<UserRights> page, UserRights userRights, Integer lang) {
        PageParam<UserRights> userRightsPage = this.page(page, new LambdaQueryWrapper<UserRights>()
                .like(!StrUtil.isBlank(userRights.getRightsName()), UserRights::getRightsName, userRights.getRightsName())
                .eq(UserRights::getStatus, 1)
                .eq(!Objects.isNull(userRights.getServiceType()), UserRights::getServiceType, userRights.getServiceType())
                .orderByDesc(UserRights::getServiceType)
                .orderByAsc(UserRights::getSeq)
        );
        List<UserRights> records = userRightsPage.getRecords();
        rightsLang(records);
        return userRightsPage;
    }

    @Override
    public List<UserRights> getRightsList() {
        List<UserRights> rights = this.list(new LambdaQueryWrapper<UserRights>()
                .eq(UserRights::getServiceType, 1)
                .orderByAsc(UserRights::getSeq)
        );
        rightsLang(rights);
        return rights;
    }

    /**
     * 权益国际化
     * @param records
     */
    private void rightsLang(List<UserRights> records){
        List<Long> rightsIdList = new ArrayList<>();
        for (UserRights record : records) {
            rightsIdList.add(record.getRightsId());
        }
        List<UserRightsLang> userRightsLangList = userRightsLangService.list(new LambdaQueryWrapper<UserRightsLang>()
                .in(UserRightsLang::getRightsId, rightsIdList));
        Map<Long, List<UserRightsLang>> longListMap = userRightsLangList.stream().collect(Collectors.groupingBy(UserRightsLang::getRightsId));
        for (UserRights record : records) {
            List<UserRightsLang> langs = longListMap.get(record.getRightsId());
            if (CollUtil.isEmpty(langs)){
                continue;
            }
            record.setUserRightsLangList(langs);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRights(UserRights userRights) {
        boolean update = this.updateById(userRights);
        // 国际化信息修改
        boolean saveOrUpdateBatch = userRightsLangService.saveOrUpdateBatch(userRights.getUserRightsLangList());
        if (update && saveOrUpdateBatch){
            return true;
        }
        return false;
//        UserRightsLang userRightsLangCn = new UserRightsLang();
//        userRightsLangCn.setRightsId(userRights.getRightsId());
//        userRightsLangCn.setLang(0);
//        userRightsLangCn.setRightsName(userRights.getRightsNameCn());
//        UserRightsLang userRightsLangZn = new UserRightsLang();
//        userRightsLangZn.setRightsId(userRights.getRightsId());
//        userRightsLangZn.setLang(1);
//        userRightsLangZn.setRightsName(userRights.getRightsNameEn());

    }
}
